package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import object.Province;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import database.ProvinceDelegate;

public class Province_Update  extends JDialog implements ActionListener {

	private JTextField txtProTName;
	private JLabel lblProTName;
	private JButton btnSave;
	private JButton btnCancel;
	private int id;
	private int rowCount;
	private String name;
	public Province_Update( JFrame main,String tp,final int id1,int row)  {

		super(main, "Sửa Tỉnh-Thành Phố", true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	    int height = 150;
	    int width = 420;
		name = tp;
		id = id1;
		rowCount = row;
	    setSize(418, 123);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
	    setResizable(false);
	    initComponet();
	}
    
	    public void initComponet(){
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		lblProTName = new JLabel("Tỉnh-Thành Phố: ");
		lblProTName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblEmTName = new GridBagConstraints();
		gbc_lblEmTName.anchor = GridBagConstraints.WEST;
		gbc_lblEmTName.insets = new Insets(15, 15, 5, 5);
		gbc_lblEmTName.fill = GridBagConstraints.VERTICAL;
		gbc_lblEmTName.gridx = 0;
		gbc_lblEmTName.gridy = 0;
		getContentPane().add(lblProTName, gbc_lblEmTName);
		
		txtProTName = new JTextField(); 
		txtProTName.setBackground(UtilityGUI.colorBgOldTxtUpdate);
		txtProTName.setForeground(UtilityGUI.colorOldTxtUpdate);
		txtProTName.setFont(UtilityGUI.fontTxtInsert);
		txtProTName.setText(name);
		txtProTName.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (txtProTName.getText().length()<1 ){
				txtProTName.setText("");
				txtProTName.setBackground(UtilityGUI.colorBgNewTxtUpdate);
				}else if (txtProTName.getText().equals(name)){
					txtProTName.setText("");
					txtProTName.setBackground(UtilityGUI.colorBgNewTxtUpdate);
				}
			}
		});
		txtProTName.addFocusListener(new FocusListener() {	
			@Override
			public void focusLost(FocusEvent e) {
				if(txtProTName.getText().length()==0 || txtProTName.getText() == null){
					txtProTName.setText(name);
					txtProTName.setBackground(UtilityGUI.colorBgOldTxtUpdate);
					txtProTName.setForeground(UtilityGUI.colorOldTxtUpdate);
				}
			}			
			@Override
			public void focusGained(FocusEvent e) {
				
			}
		});
		//add event
		txtProTName.addCaretListener(new CaretListener() {
			
			@Override
			public void caretUpdate(CaretEvent arg0) {
				ProvinceDelegate p = new ProvinceDelegate();
				if(p.CheckInsertProName((StringUtil.removeUnwantedSpace(txtProTName.getText())))){
					btnSave.setEnabled(false);
				}else{
					btnSave.setEnabled(true);
				}
				
			}
		});
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.insets = new Insets(15, 5, 5, 15);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		getContentPane().add(txtProTName, gbc_textField);
		txtProTName.setColumns(10);
		
		
		btnSave = new JButton("Lưu");
		btnSave.setEnabled(false);
		btnSave.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.VERTICAL;
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.insets = new Insets(10, 0, 5, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 2;
		getContentPane().add(btnSave, gbc_btnSave);
		btnSave.addActionListener(this);
		
		btnCancel = new JButton("Thoát");
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.fill = GridBagConstraints.BOTH;
		gbc_btnCancel.insets = new Insets(10, 0, 5, 15);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 2;
		getContentPane().add(btnCancel, gbc_btnCancel);
		btnCancel.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
			if(e.getSource()==btnSave){
				Province pro = new Province();
				pro.setProvinceId(id);
				pro.setProvinceName(txtProTName.getText());	
				ProvinceDelegate p = new ProvinceDelegate();
			    Boolean flag = p.updateProvince(pro, Main.userId);
					 if(flag == true){
					    	            this.setVisible(false);
					    				JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
										UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
							            Province_View.tblProV.getModel().setValueAt(txtProTName.getText(), rowCount, 1) ;
					  }else{
					    	 		JOptionPane.showMessageDialog(null, UtilityGUI.errorAction ,
									UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
					  }
			}else if(e.getSource()==btnCancel){
				dispose();
			}
	}


}

